<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        
        function Dad(name,age){
            this.name = name
            this.age = age
            this.money = "100000"
        }

        Dad.prototype.fn= function(){
            console.log("喜欢象棋")
        }

        function Son(name,age){
            Dad.call(this,name,age)
            this.sex = "男"
        }
        //原型深拷贝继承
        Son.prototype = deepCopy(Dad.prototype)

        //组合继承
        let Link = function(){}
        Link.prototype = Dad.prototype
        Son.prototype = new Link()
        Son.prototype.constructor = Son

        
        Son.prototype.fn = function(){
            console.log('喜欢篮球')
        }
        let zhangsan = new Son("张三",20)
        console.log(zhangsan.money)
        console.log(zhangsan.sex)
        zhangsan.fn()
        let zhangyi = new Dad("张一",30)
        zhangyi.fn()


        //深拷贝
        function deepCopy(obj){
            let newObj = Array.isArray(obj) ? [] : {}
            for(let key in obj){
                if(obj.hasOwnProperty(key)){
                    if(typeof obj[key] === "object"){
                        newObj[key] = deepCopy(obj[key])
                    }else{
                        newObj[key] = obj[key]
                    }
                }
            }
            return newObj
        }

    </script>
</body>
</html>